<template>
  <el-dialog
    title="关联列表" :visible.sync="projectDialog" append-to-body height="450" width="80%" @close="cancelDialog">
    <el-container>
      <el-header style="height: auto">
        <el-form ref="queryForm" :inline="true" :model="query" class="demo-form-inline">
          <el-form-item label="项目名称">
            <el-input v-model="query.projectName" placeholder="请输入项目名称" />
          </el-form-item>
          <el-form-item label="审计类型">
            <el-select v-model="query.auditType" clearable>
              <el-option v-for="dict in dict.type.audit_type" v-if="dict.value !== '0'" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工作年度">
            <el-date-picker placement="bottom-start"  v-model="query.year" type="year" value-format="yyyy" placeholder="选择工作年度">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch()">搜索</el-button>
            <el-button @click="searchReset()">重置</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main v-loading="loading">
        <el-table ref="projectTable" :data="data" border row-key="projectId" height="300" style="width: 100%">
          <el-table-column prop="projectCode" label="审计项目编号"></el-table-column>
          <el-table-column prop="projectName" label="审计项目名称"></el-table-column>
          <el-table-column prop="projectDate" label="工作年度" :formatter="formatProjectDate"></el-table-column>
          <el-table-column v-if="this.params.isEngineering" prop="auditedCompanyUserName" label="建设单位联系人"></el-table-column>
          <el-table-column v-if="this.params.isEngineering" prop="auditedCompanyName" label="建设单位"></el-table-column>
          <el-table-column v-if="!this.params.isEngineering" prop="auditedUserNames" label="被审计个人"></el-table-column>
          <el-table-column v-if="!this.params.isEngineering" prop="auditedCompanyName" label="被审计单位"></el-table-column>
          <el-table-column v-if="!this.params.isEngineering" prop="effectiveDate" label="审计类型">
            <template slot-scope="scope">
              <DictTag :options="dict.type.audit_type" :value="scope.row.auditType"></DictTag>
            </template>
          </el-table-column>
          <el-table-column label="项目进度" prop="projectProgress">
            <template slot-scope="scope">
              <DictTag :options="dict.type.project_progress" :value="scope.row.projectProgress"></DictTag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="relateClick(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </el-main>
    </el-container>
  </el-dialog>
</template>

<script>
import {fetchProjectList} from '@/api/adis/project/project'

export default {
  name: 'chose-project',
  dictTypes: ['audit_type', 'project_progress', 'audit_status'],
  data() {
    return {
      loading: false,
      projectDialog: false,
      query: {},
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      relateData: {},
      params: {},
    }
  },
  methods: {
    initParams(params) {
      this.params = params;
      this.projectDialog = true;
      this.onSearch();
    },
    formatProjectDate(row, column) {
      const date = row.projectDate;
      if (date) {
        return date.split('-')[0];
      }
      return '';
    },
    // 确认
    confirmDialog() {
      this.$emit('setValue', this.relateData)
      this.relateData = {}
      this.cancelDialog()
    },
    // 关闭
    cancelDialog() {
      this.projectDialog = false;
      this.$refs['queryForm'].resetFields()
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page, this.query);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      if (this.data && this.data.length === 0){
        this.loading = true;
      }
      fetchProjectList(Object.assign(params, this.query, this.params, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    relateClick(val) {
      this.relateData = val;
      this.confirmDialog();
    }
  }
}
</script>

<style>
</style>
